from django.shortcuts import render

# Create your views here.

from django.shortcuts import render

# Create your views here.
from rest_framework import viewsets, authentication
from rest_framework.pagination import PageNumberPagination
from rest_framework_jwt.authentication import JSONWebTokenAuthentication
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.permissions import IsAuthenticated

from area.models import Area
from area.serializers import AreaSerializer


class AreaPagination(PageNumberPagination):
    page_size = 10
    page_size_query_param = 'page_size'
    page_query_param = "page"



class AreaViewSet(viewsets.ModelViewSet):
    """
        赛区信息
        list:
            获取赛区信息
        create:
            添加赛区信息
        retrieve:
            获取单独赛区信息
        update:
            全部更新赛区信息
        partial_update:
            部分更新赛区信息
        delete:
            删除赛区信息
    """

    authentication_classes = (JSONWebTokenAuthentication, authentication.SessionAuthentication)  # jwt和session认证
    # pagination_class = AreaPagination  # 分页
    filter_backends = (DjangoFilterBackend,)  # 过滤，一般为choice和外键

    search_fields = ('area_name',)  # 搜索，文字字段，

    def get_serializer_class(self):
        return AreaSerializer

    def get_permissions(self):  # 动态获取权限
        if self.action in ["list", "retrieve"]:  # 查看赛区不需要登录
            return []
        return [IsAuthenticated()]

    def get_queryset(self):
        return Area.objects.filter(is_active=1)
